#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "structures.h"
#include "analyse_chaine.h"
#include "expressions.h"

void run_prog(Un_prog *pprog);

int main (int argc, char const* argv[])
{
	
	if(argc<=1){
		printf("Donnez un fichier en entree");exit(1);
	}
		
	Un_prog *prog = NULL ;
	prog = lecture_fichier(argv[1]);
	run_prog(prog);
	free(prog);
	return 0;
}

void run_prog(Un_prog *pprog){
	if(pprog->pinst==NULL)
		printf("Aucune instruction \n");
	else{
		int tmp = 0 , back ,val ;
		Une_var *pc = NULL;
		Une_inst *i = NULL;
		i = pprog->pinst;
		while(i!=NULL){
			back = 1;
			switch(i->type){
				case PRINT : if(i->pexpr->type == STR )
								 printf("%s \n",i->pexpr->str);				
							else
								printf("%d \n",evaluer_expr(i->pexpr));
							break;
				case LET :  i->pvar->val_var = evaluer_expr(i->pexpr ); 
				    
				            break;
			    case INPUT :val = scanf("%d",&tmp);
			    			i->pvar->val_var = tmp;
			    			break;
			    case GOTO	: i = i->branch_lab->pinst ;
			    			  back = 1 - back ;
			    			  break;
				case IF		: if(evaluer_expr(i->pexpr) !=0){
								i = i->branch_lab->pinst ;
								back = 1 - back ;
							}
							break;
			    			 
						
			}
			if(back)
				i= i->suiv;
		}
	}
	
	
}

